import { useGo } from '@/hooks'
import { isFunction } from 'xe-utils'
import { useRoute } from 'vue-router'
import { getAppEnvConfig } from '@/utils/env'
import { ref, unref, watch, computed, nextTick } from 'vue'
import { getAppInfo, getComRenderInfo, useComInfoStoreWithOut } from '@/store'

import { BwPerviewActionTypeEnum } from './enum'
import { BwRouterNavMenuTypeEnum } from '@/enums/routerMeta'

import type { BwToolDropdownMeunModel } from './model'
import type { MenuInfo } from 'ant-design-vue/es/menu/src/interface'

export function useFlowMain() {
  const go = useGo()
  const route = useRoute()
  const env = getAppEnvConfig()
  const formInfoStore = useComInfoStoreWithOut()

  const curKey = ref<Array<string>>(['preview'])
  const curAppId = computed<string>(() => {
    const appId = route.params.appId
    return appId ? appId.toString() : appId
  })
  const curPageId = computed<string>(() => {
    const pageId = route.params.pageId
    return pageId ? pageId.toString() : pageId
  })
  const curComName = computed<string>(() => {
    const curInfo = formInfoStore.curComInfo
    return curInfo?.name ?? '未命名组件'
  })

  const curToolMeunList = ref<Array<BwToolDropdownMeunModel>>([
    {
      id: BwPerviewActionTypeEnum.comDesign,
      name: '设计组件',
      icon: 'ant-design:file-outlined',
    },
    {
      id: BwPerviewActionTypeEnum.comSetup,
      name: '组件设置',
      icon: 'ant-design:setting-outlined',
    },
  ])

  const tpPage = (type: BwRouterNavMenuTypeEnum) => {
    const otherWindow = window?.parent
    const host = env.VITE_APP_Micro_Apps_Custom_Com
    const url = `${host}/${unref(curAppId)}/${type}/${unref(curPageId)}`

    otherWindow
      ? (otherWindow.location.href = url)
      : (window.location.href = url)
  }

  const onButtonClick = () => {
    if (unref(curAppId) && unref(curPageId)) {
      tpPage(BwRouterNavMenuTypeEnum.customComDesign)
    }
  }

  const onMenuClick = (item: MenuInfo) => {
    if (unref(curAppId) && unref(curPageId)) {
      switch (item.key) {
        case BwPerviewActionTypeEnum.comDesign: {
          tpPage(BwRouterNavMenuTypeEnum.customComDesign)
          break
        }
        case BwPerviewActionTypeEnum.comSetup: {
          tpPage(BwRouterNavMenuTypeEnum.customComSetup)
          break
        }
      }
    }
  }

  watch(
    () => curKey.value,
    (newVal) => {
      nextTick(() => {
        const otherWindow = window?.parent
        const type = newVal[0]
        if (
          'postMessage' in otherWindow &&
          isFunction(otherWindow.postMessage)
        ) {
          const host = env.VITE_APP_Micro_Apps_Main
          otherWindow.postMessage({ pageType: type }, host)
        } else {
          go(`/${unref(curAppId)}/${unref(curPageId)}/entrance/${type}`)
        }
      })
    },
    { deep: true }
  )

  watch(
    () => route,
    (newVal) => {
      if (newVal.name === 'CustomComPreview') {
        nextTick(() => {
          Promise.all([
            getAppInfo(unref(curAppId)),
            getComRenderInfo(unref(curAppId), unref(curPageId)),
          ])
        })
      }
    },
    { deep: true, immediate: true }
  )

  return {
    curKey,
    route,
    curComName,
    curToolMeunList,

    onMenuClick,
    onButtonClick,
  }
}
